package com.example.demo.juc.lock;

import lombok.SneakyThrows;

import java.util.concurrent.CountDownLatch;

/**
 * @Description: 利用它可以实现类似计数器的功能。比如有
 * 一个任务 A，它要等待其他 4 个任务执行完毕之后才能执行，此时就可以利用 CountDownLatch
 * @Author: xiatian
 * @CreateTime: 2021/9/7
 * @Company: kangxu
 */
public class CountDownLatchTest {
    public static void main(String[] args) throws InterruptedException {
        final CountDownLatch latch = new CountDownLatch(2);
        new Thread() {
            @Override
            public void run() {
                System.out.println("子线程" + Thread.currentThread().getName() + "正在执行");
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("子线程" + Thread.currentThread().getName() + "执行完成");
                latch.countDown();
            }
        }.start();
        new Thread() {
            @Override
            public void run() {
                System.out.println("子线程" + Thread.currentThread().getName() + "正在执行");
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("子线程" + Thread.currentThread().getName() + "执行完成");
                latch.countDown();
            }
        }.start();
        System.out.println("等待2个子线程执行完毕。。。");
        latch.await();
        System.out.println("等2个子线程已经执行完毕");
        System.out.println("继续执行主线程");
    }

}
